<template>
    <div style="display: flex;height: 100%;">
        <SideBar :activeItem="activeItem" :groupSettings='groupSettings' :unreadInfo='unreadInfo' class="fl" :otherGroup="isPremiss ? otherGroup : []" @unReadInfo="getStatisticsSystem()"></SideBar>
        <div  class="config-wapper fr">
          <keep-alive>
            <router-view></router-view>
          </keep-alive>
        </div>
    </div>
</template>

<script lang="ts">
import SideBar from '@/common/views/sideBar/index.vue'
import { Component, Vue } from 'vue-property-decorator'
import { commonAction } from '@/utils/common'
import { businessStore } from './store/business'
import { checkPermission } from '@/utils/directive'
@Component({
  name: 'businessMain',
  components: { SideBar }
})
export default class Config extends Vue {
  private $store: any
  private groupSettings = {
    relatedToMe: {
      title: '和我相关'
    }
  }

  private otherGroup = [{
    title: '全部工单',
    group: []
  }]

  private activeItem: any = ''
  private $route: any
  private unreadInfo = {}

  private get busCategory () {
    return businessStore.busCategory
  }

  private getStatisticsSystem () {
    const systemData = {
      accountId: this.$store.state.common.session.account._id,
      currentUser: this.$store.state.common.session.account.loginName + '@' + this.$store.state.common.session.account._id
    }
    commonAction({
      url: '/server-work/action/workOrder/statistics/system',
      method: 'post',
      data: systemData
    }).then(res => {
      this.unreadInfo = res.data
    })
  }

  private beforeRouteEnter (to: any, from: any, next: any) {
    if (from.fullPath === '/' && to.query.orderId && to.query.modelId) {
      const postData = { checkRead: false, id: to.query.orderId, modelId: to.query.modelId }
      commonAction({
        url: '/server-work/action/workOrder/search',
        method: 'post',
        data: postData
      }).then(res => {
        if (res.success) {
          const { data } = res
          const queryInfo = to.query
          if (data.orderStatus === 20) {
            next((vm: any) => {
              if (!businessStore.busCategory.length) {
                vm.getModuleCategory()
              }
              if (data.master === vm.$store.state.common.session.user._id) {
                // 我的待办
                vm.$router.push({ name: 'upcoming', query: to.query })
                vm.activeItem = '/upcoming'
              } else {
                // 全部
                vm.$router.push({ name: 'service', query: to.query })
                vm.activeItem = '/service/' + to.query.modelId
              }
            })
          } else if (data.orderStatus === 10) {
            next((vm: any) => {
              if (!businessStore.busCategory.length) {
                vm.getModuleCategory()
              }
              if (data.master === vm.$store.state.common.session.user._id) {
                // 我的待办
                vm.$router.push({ name: 'pending', query: to.query })
                vm.activeItem = '/pending'
              } else {
                // 全部
                vm.$router.push({ name: 'service', query: to.query })
                vm.activeItem = '/service/' + to.query.modelId
              }
            })
          } else {
            next((vm: any) => {
              if (!businessStore.busCategory.length) {
                vm.getModuleCategory()
              }
              vm.$router.push({ name: 'party', query: to.query })
            })
          }
        }
      })
    } else {
      next((vm: any) => {
        if (!businessStore.busCategory.length) {
          vm.getModuleCategory()
        }
      })
    }
  }

  private beforeMount () {
    // 获取工单数量
    this.getStatisticsSystem();
    (this as any).$getCache('/base/action/general/cache/query', ['departmentList'], true)
  }

  private get isPremiss () {
    return checkPermission(null, { value: ['workorder_mgt_all'] }, 'limits')
  }

  private getModuleCategory () {
    commonAction({
      url: '/server-work/action/workOrderModel/getCategory/read',
      method: 'post',
      data: {}
    }).then(res => {
      if (res.success) {
        this.handleData(res.data || [])
      }
    })
  }

  private handleData (data) {
    businessStore.busCategory = data
    const arr = [] as any
    for (let i = 0; i < data.length; i++) {
      arr.push({
        path: 'service/' + data[i].id,
        name: 'service',
        meta: {
          title: data[i].modelName,
          group: 'allBusiness'
        },
        component: () => import(/* webpackChunkName: "number" */ '@/views/config/number/index.vue')
      })
    }
    if (arr.length) {
      this.otherGroup[0].group = arr
    }
  }
}
</script>

<style lang="stylus" scoped>
  .config-wapper
      width calc(100vw - 262px)
      height 100%
      flex 1
      padding-left 16px
  .navMenuState .config-wapper
      width calc(100vw - 314px)
</style>
